﻿using UnityEngine;
using Zero;

/// <summary>
/// 元素反应：结晶
/// </summary>
public class ElementCrystallize : IElementReaction
{
    private readonly IElementHolder _holder;

    public ElementCrystallize(IElementHolder holder)
    {
        _holder = holder;
    }

    public void Reaction(TriggerElementData triggerElementData)
    {
        if (triggerElementData.ElementType != ElementType.Rock)
        {
            return;
        }

        // todo: 原神中结晶有1秒的CD
        TryCrystallize(ElementType.Fire, triggerElementData);
        TryCrystallize(ElementType.Water, triggerElementData);
        TryCrystallize(ElementType.Electric, triggerElementData);
        TryCrystallize(ElementType.Ice, triggerElementData);

        if (triggerElementData.IsTriggered)
        {
            ServiceLocator.Get<IWorldNumberService>().CreateElementReaction(_holder.Owner.transform.position, "结晶");
        }
    }

    private void TryCrystallize(ElementType elementType, TriggerElementData triggerElementData)
    {
        if (triggerElementData.Gauge <= 0f)
        {
            return;
        }

        var aura = _holder.GetGeneralElementAura(elementType);
        if (aura.Gauge <= 0f)
        {
            return;
        }

        triggerElementData.IsTriggered = true;

        var cost = triggerElementData.Gauge * 0.5f;

        // 消耗触发元素
        triggerElementData.Gauge -= aura.Gauge * 2f;

        // 消耗附着元素
        aura.UseGauge(cost);

        Create(elementType);
    }

    private void Create(ElementType elementType)
    {
        Debug.Log($"创建结晶: {elementType}");
    }
}